var map, viaMarker;
$(function () {
    storeAddress()
})

var provinceId, cityName, form, searchName;
layui.use('form', function () {
    form = layui.form;
    cityName = $("#city option:selected").val();
    provinceId = $("#province option:selected").val();
    form.on('select(province)', function (data) {
        //1:拿到select对象：
        provinceId = $('#province').find('option:selected').val();
        city(provinceId);
        form.render('select');
    });

    form.on('select(city)', function (data) {
        console.log(data)
        cityName = $("#city option:selected").val();
        console.log(cityName)
        searchAddress(cityName)

        // form.render('select');
    })
});

function searchAddress(cityName) {
    var addressHtml = '';
    postJSON("/storeQuery/storeAddress", {"cityName": cityName}, function (res) {
        $(".address-main").empty();
        if (res.data.length > 0) {
            $.each(res.data, function (index, item) {
                addressHtml = $('<div class="address-content" id="pantoBtn" data-lat="' + item.latitude + '" data-lng="' + item.longitude + '" onclick="addressBut(this)">\n' +
                    '                        <div class="address-title">' + item.name + '</div>\n' +
                    '                        <div class="address">' + item.address + '</div>\n' +
                    '                    </div>');
            })
        } else {
            addressHtml = $('<div>\n' +
                '                        <div class="address-title">暂时该地区还没有门店地址</div>\n' + '</div>');
        }
        $(".address-main").append(addressHtml);
    })
}

//获取市
function city(value) {
    var $city = $("#city");
    var city;
    postJSON("/storeQuery/city", {"provinceCode": value}, function (res) {
        $city.empty();
        $.each(res.data, function (index, item) {
            city = $('<option value="' + item.city_name + '">' + item.city_name + '</option>');
            $city.append(city);
        })
        searchName = res.data[0].city_name;
        form.render('select');
        cityName = $("#city option:selected").val();
        console.log(cityName, searchName)
        searchAddress(cityName)
    })

}

//获取全部地址
function storeAddress() {
    var addressHtml = '';
    postJSON("/storeQuery/storeAddress", {}, function (res) {
        console.log(res.data[0].longitude, res.data[0].latitude)
        $(".address-main").empty();
        $.each(res.data, function (index, item) {
            addressHtml = $('<div class="address-content" id="pantoBtn" data-lat="' + item.latitude + '" data-lng="' + item.longitude + '" onclick="addressBut(this)">\n' +
                '                        <div class="address-title">' + item.name + '</div>\n' +
                '                        <div class="address">' + item.address + '</div>\n' +
                '                    </div>');
            $(".address-main").append(addressHtml);
        })

        map = new AMap.Map("container", {
            resizeEnable: true,
            center: [res.data[0].longitude, res.data[0].latitude],
            zoom: 13
        });

        viaMarker = new AMap.Marker({
            position: new AMap.LngLat(res.data[0].longitude, res.data[0].latitude),
            // 将一张图片的地址设置为 icon
            icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png',
            // 设置了 icon 以后，设置 icon 的偏移量，以 icon 的 [center bottom] 为原点
            offset: new AMap.Pixel(-13, -30)
        });

        // 将 markers 添加到地图
        map.add(viaMarker);
    })

}

function addressBut(that) {
    console.log(1111)
    var latitude = $(that).data("lat");
    var longitude = $(that).data("lng");
    console.log(latitude, longitude)
    map.panTo([longitude, latitude]);
    viaMarker.setMap(null);
    viaMarker = null;
    // 创建一个 Icon
    viaMarker = new AMap.Marker({
        position: new AMap.LngLat(longitude, latitude),
        // 将一张图片的地址设置为 icon
        icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png',
        // 设置了 icon 以后，设置 icon 的偏移量，以 icon 的 [center bottom] 为原点
        offset: new AMap.Pixel(-13, -30)
    });

    // 将 markers 添加到地图
    map.add(viaMarker);
}
